<?php
/*
**智游宝退款通知
**2015-06-17
**by pzp
*/
$inSide = new SoapClient(null,array(
        "location" => "http://localhost/open/openService/pft_insideMX.php",
        "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
);

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}
$retreatBatchNo =$_REQUEST['retreatBatchNo'];//退票批次号
$pft_ordernum = $_REQUEST['orderCode'];//票付通订单号
$returnNum = $_REQUEST['returnNum'];//退票数量
$auditStatus = $_REQUEST['auditStatus']; //审核状态:失败/成功
$sign = $_REQUEST['sign'];

$txt=json_encode($_REQUEST);
logfile($txt,"zyb_return_order_data");

/*
{"retreatBatchNo":"710d3cdc58b24b4594b813fcedaf3bd3","orderCode":"pft144333482967625357","subOrderCode":"pft144333482967625357","auditStatus":"success","returnNum":"2","sign":"e0f97a9884475f5603c3fbddf7b89269"}
http://www.12301.cc/module/api/ZhiYouBao/zyb_Return_oStatus.php?orderCode=pft144465217512478502&subOrderCode=pft144465217512478502&auditStatus=success&returnNum=1&sign=c36666272ec9cfcf8632226ce670b667&retreatBatchNo=710d3cdc58b24b4594b813fcedaf3bd3
*/

if(!$retreatBatchNo) exit;
if($auditStatus == 'success'){
	
	// $domain = explode('pft',$pft_ordernum));
	// if(count($domain)<2){ echo 'fail';	exit;} // 不是票付通的订单
	
	$selorder = "select pftOrder from all_api_order where tempOrder='$pft_ordernum' limit 1";
	$GLOBALS['le']->query($selorder);
	$GLOBALS['le']->fetch_assoc();
	$pftOrder = $GLOBALS['le']->f('pftOrder');
	
	$pft_order = $inSide->Order_Globle_Search(
			0,0,0,0,"",
			"","","","","",
			"","",$pftOrder,"",0,
			"","","",1,0,
			0,1,"","","",
			"","","","","",
			"","","1","","");
	if(strlen($pft_order)>50){
		$pft_order_xml =simplexml_load_string($pft_order);
		// print_r($pft_order_xml);exit;
		$UUlid = (int)$pft_order_xml->Rec->UUlid;
		$UUtnum = (int)$pft_order_xml->Rec->UUtnum;
		$UUtprice = (string)$pft_order_xml->Rec->UUtprice;
		$UUaids=	(string)$pft_order_xml->Rec->UUaids;
		if($UUaids == 0){
			$UUaid = (int)$pft_order_xml->Rec->UUaid;
		}else{
			$UUaidss= explode(',',$UUaids);
			$UUaid = $UUaidss[0];
		}
		
		switch ($UUaid){ 		
			case 3311:  //中赫
				$key = "sdzfxhlwh";
			break;
			
			case 22415:
				$key = "sdzfxsdhyls";
			break;
			
			case 37809:  //南山大佛
				$key = "sdzfxlypw";
			break;
			
			case 51667:
				$key = '3665D15A9D73DF3506F51CFD013ABC3B';
			break;
			
			case 50618: //驴妈妈
				$key = 'sdzfxlvmamafx';
			break;
			
			case 22845: //乐游票付通 
				$key = 'sdzfxxmlyjr';
			break;
			
			case 4274: //携程分销
				$key = 'xfcxhs';
			break;
			
			case 27810: //先行网运营部
				$key = '6265C757118A935909641669CF12B212';
			break;
			
			default:
			break;
		}
		$checksign = md5($pft_ordernum.$key);
		if($sign == $checksign){
			if($returnNum){
				$synum = $UUtnum - $returnNum; //剩余数量
				if($synum == 0){
					$selorder = "update all_api_order set oStatus = 3  where tempOrder='$pft_ordernum' limit 1";
					$GLOBALS['le']->query($selorder);
				}
				if($synum > 0){
					$selorder = "update all_api_order set oStatus = 4,oStnum = $synum where tempOrder='$pft_ordernum' limit 1";
					$GLOBALS['le']->query($selorder);
				}
				$money = $synum * $UUtprice;  //剩余总额
				$Cancel = $inSide->Order_Change_Pro($pftOrder,$synum,$money,0,0,"","","");
				if($Cancel == 100){
					logfile($pftOrder.'|'.$Cancel,'zyb_modify_success');
					echo 'success';
				}
				else{
					logfile($pftOrder.'|'.$Cancel,'zyb_modify_fail');
					echo 'fail';
				}
			}
		}
	}
}

function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		// $word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
?> 